package com.wmh.baseservice.skd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wmh.baseservice.skd.pojo.SkdGoodsLabel;
import com.wmh.baseservice.skd.service.SkdGoodsLabelService;
import com.wmh.baseservice.skd.mapper.SkdGoodsLabelMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author CH
* @description 针对表【skd_goods_label】的数据库操作Service实现
* @createDate 2023-12-24 14:37:07
*/
@Service
public class SkdGoodsLabelServiceImpl extends ServiceImpl<SkdGoodsLabelMapper, SkdGoodsLabel>
    implements SkdGoodsLabelService{

    @Resource
    private SkdGoodsLabelMapper skdGoodsLabelMapper;

    /**
     * 添加商品标签
     * @param usId 用户id
     * @param label 商品标签
     */
    @Override
    public void addGoodsLabel(Long usId, String label) {
        SkdGoodsLabel skdGoodsLabel = new SkdGoodsLabel();
        skdGoodsLabel.setUsId(usId).setLabelName(label);
        skdGoodsLabelMapper.insert(skdGoodsLabel);
    }

    /**
     * 根据用户id查询商品标签列表
     * @param usId 用户id
     * @return 返回商品标签列表
     */
    @Override
    public List<Map<String, Object>> queryLabelsByUsId(Long usId) {
        List<Map<String, Object>> labelList = new ArrayList<>();
        QueryWrapper<SkdGoodsLabel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("us_id", usId);
        List<SkdGoodsLabel> skdGoodsLabels = skdGoodsLabelMapper.selectList(queryWrapper);
        skdGoodsLabels.forEach(e -> {
            Map<String, Object> labelMap = new HashMap<>();
            labelMap.put("id", e.getId());
            labelMap.put("labelName", e.getLabelName());
            labelList.add(labelMap);
        });
        return labelList;
    }

    /**
     * 删除商品标签
     * @param usId 用户id
     * @param label 标签
     */
    @Override
    public void deleteLabel(Long usId, String label) {
        QueryWrapper<SkdGoodsLabel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("us_id", usId);
        queryWrapper.eq("label_name", label);
        skdGoodsLabelMapper.delete(queryWrapper);
    }

    /**
     * 根据用户id和商品标签查询是否存在该标签
     * @param usId 用户id
     * @param label 商品标签
     * @return 存在该标签返回true, 不存在返回false
     */
    @Override
    public boolean selectLabelByNameAndUsId(Long usId, String label) {
        // 创建查询条件
        QueryWrapper<SkdGoodsLabel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("us_id", usId);
        queryWrapper.eq("label_name", label);

        // 查询数据库
        SkdGoodsLabel existingLabel = skdGoodsLabelMapper.selectOne(queryWrapper);

        // 如果存在同名标签, 返回 true; 否则, 返回 false
        return existingLabel != null;
    }
}




